<?php require_once('Connections/db_conn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


mysql_select_db($database_db_conn, $db_conn);
$query_Community_Partner = "SELECT ORGNAME FROM organization ORDER BY ORGNAME ASC";
$Community_Partner = mysql_query($query_Community_Partner, $db_conn) or die(mysql_error());
$row_Community_Partner = mysql_fetch_assoc($Community_Partner);
$totalRows_Community_Partner = mysql_num_rows($Community_Partner);

mysql_select_db($database_db_conn, $db_conn);
$query_Presenter = "SELECT FIRSTNAME, LASTNAME FROM presenter ORDER BY LASTNAME ASC";
$Presenter = mysql_query($query_Presenter, $db_conn) or die(mysql_error());
$row_Presenter = mysql_fetch_assoc($Presenter);
$totalRows_Presenter = mysql_num_rows($Presenter);

$query_Language = "SELECT lang.LANGID LANGID, lang.LANG LANG FROM `language` lang ORDER BY LANG ASC";
$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
$layout = explode('&&&', file_get_contents('layout.html'));
echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");
?>

<html>
<body>
<script>
function showForm (obj)
{
	obj.style.display = "block";
}


function hideForm(obj)
{
	obj.style.display = "none";
}
</script>
<!-- Begin Main Column -->

<div id="mainContent">

	<h2>Step 2: Presenter Information</h2>
 <p></p>
    Note: Required fields denoted with an asterisk(*)
	<form id="form1" method="post" action="">
    	<p>Select a Presenter</p>
	<table width="465" height="46" border="0">
      <tr>
        <td width="185">Presenter:</td>
        <td width="270">
          <p>
            <label>
            <select name="presenter" id="presenter">
              <option value="None" selected="selected" onclick="showForm(getElementById('form2'));" <?php if (!(strcmp("None", "None"))) {echo "selected=\"selected\"";} ?>>None</option>
              <?php
do {
?>
              <option value="<?php echo $row_Presenter['FIRSTNAME']." ".$row_Presenter['LASTNAME']?>"<?php if (!(strcmp($row_Presenter['FIRSTNAME'], "None"))) {echo "selected=\"selected\"";} ?> onclick="hideForm(getElementById('form2'));" ><?php echo $row_Presenter['FIRSTNAME']." ".$row_Presenter['LASTNAME']?></option>
              <?php
} while ($row_Presenter = mysql_fetch_assoc($Presenter));
  $rows = mysql_num_rows($Presenter);
  if($rows > 0) {
      mysql_data_seek($Presenter, 0);
	  $row_Presenter = mysql_fetch_assoc($Presenter);
  }
?>
            </select>
            </label>
          </p>
        </td>
      </tr>
    </table>
    <p>
		<label>
	    	<input type="submit" name="submit1" id="submit1" value="Submit" class = "button"/>
		</label>
	</p>
    </form>

	<form id="form2" method="post" action="" style="display:block;">
	  <p>--OR--</p>
	<p>Add a Presenter</p>
	  <table width="470" height="286" border="0">
        <tr>
          <td width="184">* First Name:</td>
          <td width="276"><label>
            <input type="text" name="first_name" id="first_name" />
          </label></td>
        </tr>
        <tr>
          <td>* Last Name:</td>
          <td><label>
            <input type="text" name="last_name" id="last_name" />
          </label></td>
        </tr>
        <tr>
          <td>* Address:</td>
          <td><label>
            <input type="text" name="address" id="address" />
          </label></td>
        </tr>
        <tr>
          <td>City:</td>
          <td><label>
            <input type="text" name="city" id="city" />
          </label></td>
        </tr>
        <tr>
          <td>Zipcode:</td>
          <td><label>
            <input name="zipcode" type="text" id="zipcode" size="5" maxlength="5" />
          </label></td>
        </tr>
        <tr>
          <td>Phone:</td>
          <td><label>
            <input name="phone" type="text" id="phone" size="10" maxlength="10" />
          </label></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><label>
            <input type="text" name="email" id="email" />
          </label></td>
        </tr>
        <tr>
          <td>Fax:</td>
          <td><label>
            <input name="fax" type="text" id="fax" size="10" maxlength="10" />
          </label></td>
        </tr>
        <tr>
          <td>Organization</td>
          <td><label>
            <select name="organization" id="organization">
              <?php
do {
?><option value="<?php echo $row_Community_Partner['ORGNAME']?>"><?php echo $row_Community_Partner['ORGNAME']?></option>
              <?php
} while ($row_Community_Partner = mysql_fetch_assoc($Community_Partner));
  $rows = mysql_num_rows($Community_Partner);
  if($rows > 0) {
      mysql_data_seek($Community_Partner, 0);
	  $row_Community_Partner = mysql_fetch_assoc($Community_Partner);
  }
?>
          </select>
          </label></td>
        </tr>
        <tr>
        <td>Languages:</td>
        <td>
        <?php
			  while ($row_Language = mysql_fetch_assoc($Language)){
			  echo "<input type='checkbox' name='presenter_lang[]' value='".$row_Language['LANGID']."' $pres_lang />   ".$row_Language['LANG']."<br />";
			  }
		  ?>
        Other:
        <input type="text" name="language_other" id="language_other" />
        </td>
      </tr>
      <tr>
        <td>Contract Trainer:</td>
        <td>
            <input type="radio" name="contract_trainer" value="Yes" id="contract_trainer_yes" />
            Yes
            <input type="radio" name="contract_trainer" value="No" id="contract_trainer_no" />
            No
        </td>
      </tr>

      </table>
      <p>
	    <label>
	      <input type="submit" name="submit2" id="submit2" value="Submit" class = "button"/>
        </label>
      </p>
  </form>

     <?php

   		if($_POST) {
		ob_start();
		$host="localhost"; // Host name
		$username="root"; // Mysql username
		$password=""; // Mysql password
		$db_name="rtl"; // Database name
		$tbl_name="presenter"; // Table name

		// Connect to server and select databse.
		mysql_connect("$host", "$username", "$password")or die("cannot connect");
		mysql_select_db("$db_name")or die("cannot select DB");

		//Get workshop id
		$workshop_id = $_GET['id'];

		if($_POST[submit1])
		{
			$presenter=$_POST['presenter'];
			$token = strtok($presenter, " ");
			$first_name = $token;
			$token = strtok(" ");
			$last_name  = $token;
		}
		else
		{
			$first_name=$_POST['first_name'];
			$last_name=$_POST['last_name'];
			$address=$_POST['address'];
			$city=$_POST['city'];
			$zipcode=$_POST['zipcode'];
			$phone=$_POST['phone'];
			$email=$_POST['email'];
			$fax=$_POST['fax'];
			$organization=$_POST['organization'];
			$contract_trainer=$_POST['contract_trainer'];
			$language_other = $_POST['language_other'];
			$presenter_lang = $_POST["presenter_lang"];


			// To protect MySQL injection (more detail about MySQL injection)
			$first_name = stripslashes($first_name);
			$last_name = stripslashes($last_name);
			$address = stripslashes($address);
			$city = stripslashes($city);
			$zipcode = stripslashes($zipcode);
			$phone = stripslashes($phone);
			$email = stripslashes($email);
			$fax = stripslashes($fax);
			$language_other = stripslashes($language_other);
			//$organization=stripslashes($organization);

			$first_name = mysql_real_escape_string($first_name);
			$last_name = mysql_real_escape_string($last_name);
			$address = mysql_real_escape_string($address);
			$city = mysql_real_escape_string($city);
			$zipcode = mysql_real_escape_string($zipcode);
			$phone = mysql_real_escape_string($phone);
			$email = mysql_real_escape_string($email);
			$fax = mysql_real_escape_string($fax);
			$language_other = mysql_real_escape_string($language_other);

			//$organization=mysql_real_escape_string($organization);

			$error=0;
			if($first_name != NULL && $last_name !=NULL)
			{
				$query_Presenter = "SELECT * FROM presenter";
				$row_Presenter = mysql_query($query_Presenter, $db_conn) or die(mysql_error());
				while ($Presenter_all = mysql_fetch_assoc($row_Presenter)){
					if($Presenter_all['FIRSTNAME'] == $first_name && $Presenter_all['LASTNAME'] == $last_name
						&& $Presenter_all['ADDRESS'] == $address)
					{
						$present_yes = 1;
						break;
					}
					else
					{
						$present_yes = 0;
					}
				}
				if($present_yes ==0)
				{
					$get_org_id = mysql_query("SELECT ORGID FROM ORGANIZATION WHERE ORGNAME='$organization'");
					$org_id = mysql_result($get_org_id,0);

					mysql_query("INSERT INTO
						$tbl_name (ORGID, FIRSTNAME, LASTNAME, ADDRESS, CITY, ZIPCODE, PHONENO, EMAIL, FAX_NO, CONTRACTTRAINER)
						VALUES ('$org_id', '$first_name', '$last_name', '$address', '$city', '$zipcode', '$phone', '$email', '$fax',
						'$contract_trainer')");

					$get_presenter_id=mysql_query("select@@IDENTITY");
					$presenter_id=mysql_result($get_presenter_id,0);

					if (!empty($presenter_lang))
					{
						foreach ($presenter_lang as $k => $v)
						{
							$sql_lang = "INSERT INTO PRESENTERLANGUAGE (LANGID , PRESENTERID ) VALUES ($v,$presenter_id)";
								if (mysql_query($sql_lang, $db_conn))
									echo "Language has been updated";
								else
								{
									echo("<P>Error adding Language: $sql_lang " .
						            mysql_error() . "</P>");
								}

						}
					}

					$query_Language = "SELECT LANG FROM language";
					$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
					if($language_other != NULL)
					{
						while ($row_Language = mysql_fetch_assoc($Language)){
							if($row_Language['LANG'] == $language_other)
							{
								$lang_yes = 1;
								break;
							}
							else
							{
								$lang_yes = 0;
							}
						}
			  			if($lang_yes == 0)
			  			{
							  mysql_query("INSERT INTO language (LANG) VALUES ('$language_other')");
							  $get_lang_id=mysql_query("select@@IDENTITY");
				              $lang_id=mysql_result($get_lang_id,0);
							  mysql_query("INSERT INTO presenterlanguage (LANGID, PRESENTERID) VALUES ('$lang_id', '$presenter_id')");
						}
					}
				}
			}
		}
		$get_presenter_id = mysql_query("SELECT PRESENTERID FROM PRESENTER WHERE FIRSTNAME='$first_name' AND LASTNAME = '$last_name'");
		$presenter_id = mysql_result($get_presenter_id,0);
		mysql_query("UPDATE WORKSHOP SET PRESENTERID = '$presenter_id' WHERE WRKSPID = '$workshop_id'");

		if(!$error) {
			echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id'>";
		}
		ob_end_flush();
   }
   ?>

</div>

<!-- Begin Side Column -->
<!-- Begin Footer -->

<?php
echo $layout[1];
?>

</body>
</html>
<?php
mysql_free_result($Community_Partner);

mysql_free_result($Presenter);
?>
